package jun.carlauncher.utils;

/**
 * Created by NocodeLee on 2017/10/26.
 * Email：lirizhilirizhi@163.com
 */

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import java.io.File;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/09/23
 *     desc  : 意图相关工具类
 * </pre>
 */
public final class IntentUtils {

  private IntentUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  /**
   * 获取安装App（支持7.0）的意图
   *
   * @param filePath  文件路径
   * @param authority 7.0及以上安装需要传入清单文件中的{@code <provider>}的authorities属性
   *                  <br>参看https://developer.android.com/reference/android/support/v4/content/FileProvider.html
   * @return intent
   */
  //public static Intent getInstallAppIntent(final String filePath, final String authority) {
  //  return getInstallAppIntent(FileUtils.getFileByPath(filePath), authority);
  //}

  /**
   * 获取安装App(支持7.0)的意图
   *
   * @param file      文件
   * @param authority 7.0及以上安装需要传入清单文件中的{@code <provider>}的authorities属性
   *                  <br>参看https://developer.android.com/reference/android/support/v4/content/FileProvider.html
   * @return intent
   */
  public static Intent getInstallAppIntent(final File file, final String authority) {
    if (file == null) return null;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data;
    String type = "application/vnd.android.package-archive";
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
      data = Uri.fromFile(file);
    } else {
      intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      data = FileProvider.getUriForFile(Utils.getApp(), authority, file);
    }
    intent.setDataAndType(data, type);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取卸载App的意图
   *
   * @param packageName 包名
   * @return intent
   */
  public static Intent getUninstallAppIntent(final String packageName) {
    Intent intent = new Intent(Intent.ACTION_DELETE);
    intent.setData(Uri.parse("package:" + packageName));
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取打开App的意图
   *
   * @param packageName 包名
   * @return intent
   */
  public static Intent getLaunchAppIntent(final String packageName) {
    return Utils.getApp().getPackageManager().getLaunchIntentForPackage(packageName);
  }

  /**
   * 获取App具体设置的意图
   *
   * @param packageName 包名
   * @return intent
   */
  public static Intent getAppDetailsSettingsIntent(final String packageName) {
    Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
    intent.setData(Uri.parse("package:" + packageName));
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取分享文本的意图
   *
   * @param content 分享文本
   * @return intent
   */
  public static Intent getShareTextIntent(final String content) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, content);
    return intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取分享图片的意图
   *
   * @param content   文本
   * @param imagePath 图片文件路径
   * @return intent
   */
  public static Intent getShareImageIntent(final String content, final String imagePath) {
    return getShareImageIntent(content, FileUtils.getFileByPath(imagePath));
  }

  /**
   * 获取分享图片的意图
   *
   * @param content 文本
   * @param image   图片文件
   * @return intent
   */
  public static Intent getShareImageIntent(final String content, final File image) {
    if (!FileUtils.isFileExists(image)) return null;
    return getShareImageIntent(content, Uri.fromFile(image));
  }

  /**
   * 获取分享图片的意图
   *
   * @param content 分享文本
   * @param uri     图片uri
   * @return intent
   */
  public static Intent getShareImageIntent(final String content, final Uri uri) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, content);
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    intent.setType("image/*");
    return intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取其他应用组件的意图
   *
   * @param packageName 包名
   * @param className   全类名
   * @return intent
   */
  public static Intent getComponentIntent(final String packageName, final String className) {
    return getComponentIntent(packageName, className, null);
  }

  /**
   * 获取其他应用组件的意图
   *
   * @param packageName 包名
   * @param className   全类名
   * @param bundle      bundle
   * @return intent
   */
  public static Intent getComponentIntent(final String packageName, final String className, final Bundle bundle) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    if (bundle != null) intent.putExtras(bundle);
    ComponentName cn = new ComponentName(packageName, className);
    intent.setComponent(cn);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取关机的意图
   * <p>需添加权限 {@code <uses-permission android:name="android.permission.SHUTDOWN"/>}</p>
   *
   * @return intent
   */
  public static Intent getShutdownIntent() {
    Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取跳至拨号界面意图
   *
   * @param phoneNumber 电话号码
   */
  public static Intent getDialIntent(final String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取拨打电话意图
   * <p>需添加权限 {@code <uses-permission android:name="android.permission.CALL_PHONE"/>}</p>
   *
   * @param phoneNumber 电话号码
   */
  public static Intent getCallIntent(final String phoneNumber) {
    Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + phoneNumber));
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  /**
   * 获取跳至发送短信界面的意图
   *
   * @param phoneNumber 接收号码
   * @param content     短信内容
   */
  public static Intent getSendSmsIntent(final String phoneNumber, final String content) {
    Uri uri = Uri.parse("smsto:" + phoneNumber);
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
    intent.putExtra("sms_body", content);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  }


  /**
   * 获取拍照的意图
   *
   * @param outUri 输出的uri
   * @return 拍照的意图
   */
  public static Intent getCaptureIntent(final Uri outUri) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
    return intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
  }

  //    /**
  //     * 获取选择照片的Intent
  //     *
  //     * @return
  //     */
  //    public static Intent getPickIntentWithGallery() {
  //        Intent intent = new Intent(Intent.ACTION_PICK);
  //        return intent.setType("image*//*");
  //    }
  //
  //    /**
  //     * 获取从文件中选择照片的Intent
  //     *
  //     * @return
  //     */
  //    public static Intent getPickIntentWithDocuments() {
  //        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  //        return intent.setType("image*//*");
  //    }
  //
  //
  //    public static Intent buildImageGetIntent(final Uri saveTo, final int outputX, final int outputY, final boolean returnData) {
  //        return buildImageGetIntent(saveTo, 1, 1, outputX, outputY, returnData);
  //    }
  //
  //    public static Intent buildImageGetIntent(Uri saveTo, int aspectX, int aspectY,
  //                                             int outputX, int outputY, boolean returnData) {
  //        Intent intent = new Intent();
  //        if (Build.VERSION.SDK_INT < 19) {
  //            intent.setAction(Intent.ACTION_GET_CONTENT);
  //        } else {
  //            intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
  //            intent.addCategory(Intent.CATEGORY_OPENABLE);
  //        }
  //        intent.setType("image*//*");
  //        intent.putExtra("output", saveTo);
  //        intent.putExtra("aspectX", aspectX);
  //        intent.putExtra("aspectY", aspectY);
  //        intent.putExtra("outputX", outputX);
  //        intent.putExtra("outputY", outputY);
  //        intent.putExtra("scale", true);
  //        intent.putExtra("return-data", returnData);
  //        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
  //        return intent;
  //    }
  //
  //    public static Intent buildImageCropIntent(final Uri uriFrom, final Uri uriTo, final int outputX, final int outputY, final boolean returnData) {
  //        return buildImageCropIntent(uriFrom, uriTo, 1, 1, outputX, outputY, returnData);
  //    }
  //
  //    public static Intent buildImageCropIntent(Uri uriFrom, Uri uriTo, int aspectX, int aspectY,
  //                                              int outputX, int outputY, boolean returnData) {
  //        Intent intent = new Intent("com.android.camera.action.CROP");
  //        intent.setDataAndType(uriFrom, "image*//*");
  //        intent.putExtra("crop", "true");
  //        intent.putExtra("output", uriTo);
  //        intent.putExtra("aspectX", aspectX);
  //        intent.putExtra("aspectY", aspectY);
  //        intent.putExtra("outputX", outputX);
  //        intent.putExtra("outputY", outputY);
  //        intent.putExtra("scale", true);
  //        intent.putExtra("return-data", returnData);
  //        intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
  //        return intent;
  //    }
  //
  //    public static Intent buildImageCaptureIntent(final Uri uri) {
  //        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  //        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  //        return intent;
  //    }
}
